package com.pda.common.pages.inscanf;

import android.content.Context;

import com.apkfuns.logutils.LogUtils;
import com.pda.common.basemvp.model.BaseModel;
import com.pda.common.bean.BaseBean;
import com.pda.common.bean.BillBean;
import com.pda.common.beanin.ScanfInBean;
import com.pda.common.database.BillInfo;
import com.pda.common.database.CodeInfo;
import com.pda.common.database.DataBaseControl;
import com.pda.common.tools.StringProcess;
import com.pda.common.utils.PDAUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class InScanfModelImpl extends BaseModel implements InScanfModel {
    private String TAG = "InScanfModelImpl";
    private BaseBean baseBean = null;
    BillBean billBean = new BillBean();
    CodeInfo codeInfo = new CodeInfo();

    public InScanfModelImpl(Context context) {
        super(context);
    }

    @Override
    public BillBean addCode(final ScanfInBean scanfInBean, final CallBack callBack) {
        codeInfo.setBill(scanfInBean.getBillID());
        codeInfo.setCode(scanfInBean.getCode());
        codeInfo.setType(PDAUtil.IN);
        RequestBody body = RequestBody.create(PDAUtil.JSON, gson.toJson(scanfInBean));
        billBean = new BillBean();
        final Call<ResponseBody> call1 = httpConfigsync.commitCodesync(body);

        try {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response<ResponseBody> response = call1.execute();
                        if (!response.isSuccessful()) {
                            billBean.setErrcode(-2);
                            billBean.setErrmsg(PDAUtil.connectError);
                        } else {
                            billBean = gson.fromJson(StringProcess.responsetoString(response.body()), BillBean.class);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        billBean.setErrcode(-2);
                        billBean.setErrmsg(PDAUtil.connectError);
                    }
                }
            });
            thread.start();
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
            billBean.setErrcode(-2);
            billBean.setErrmsg(PDAUtil.connectError);
        } finally {
            return billBean;
        }
//            httpConfig.commitCode(body)
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
//                        private Disposable mDisposable;
//
//                        @Override
//                        public void onSubscribe(Disposable d) {
//                            mDisposable = d;
//                        }
//
//                        @Override
//                        public void onComplete() {
//                            callBack.onshowres("数码:"+ scanfInBean.getCode()+"入库成功");
//                            callBack.onupdate(BillBean);
//                            mDisposable.dispose();
//                        }
//
//                        @Override
//                        public void onError(Throwable e) {
//                            DataBaseControl.deleteCode(codeInfo);
//                            if (BillBean.getErrmsg() != null && BillBean.getErrmsg().length() > 0) {
//                                callBack.onshowres(BillBean.getErrmsg());
//                            } else {
//                                callBack.onshowres(PDAUtil.connectError);
//                            }
//                            mDisposable.dispose();
//                        }
//
//                        @Override
//                        public void onNext(ResponseBody abBean) {
//                            LogUtils.w(StringProcess.responsetoString(abBean));
//                            BillBean = gson.fromJson(StringProcess.responsetoString(abBean), BillBean.class);
//                            if (BillBean != null && BillBean.getErrcode() == 0) {
//                                onComplete();
//                            } else {
//                                onError(new Throwable());
//                            }
//                        }
//                    });
//        } catch (Exception e) {
//            DataBaseControl.deleteCode(codeInfo);
//            callBack.onshowres(PDAUtil.connectError);
//            e.printStackTrace();
//        }
    }

    @Override
    public BillBean deleteCode(final ScanfInBean scanfInBean, final CallBack callBack) {
        codeInfo.setBill(scanfInBean.getBillID());
        codeInfo.setCode(scanfInBean.getCode());
        codeInfo.setType(PDAUtil.IN);
        RequestBody body = RequestBody.create(PDAUtil.JSON, gson.toJson(scanfInBean));
        billBean = new BillBean();
        final Call<ResponseBody> call1 = httpConfigsync.deleteCodesync(body);
        try {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response<ResponseBody> response = call1.execute();
                        if (!response.isSuccessful()) {
                            billBean.setErrcode(-2);
                            billBean.setErrmsg(PDAUtil.connectError);
                        } else {
                            billBean = gson.fromJson(StringProcess.responsetoString(response.body()), BillBean.class);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        billBean.setErrcode(-2);
                        billBean.setErrmsg(PDAUtil.connectError);
                    }
                }
            });
            thread.start();
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
            billBean.setErrcode(-2);
            billBean.setErrmsg(PDAUtil.connectError);
        } finally {
            return billBean;
        }
//        try {
//            RequestBody body = RequestBody.create(PDAUtil.JSON, gson.toJson(scanfInBean));
//            httpConfig.deleteCode(body)
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
//                        private Disposable mDisposable;
//
//                        @Override
//                        public void onSubscribe(Disposable d) {
//                            mDisposable = d;
//                        }
//
//                        @Override
//                        public void onComplete() {
//                            callBack.onshowres("数码:"+ scanfInBean.getCode()+"删除成功");
//                            callBack.onupdate(BillBean);
//                            mDisposable.dispose();
//                        }
//
//                        @Override
//                        public void onError(Throwable e) {
//                            if (BillBean.getErrmsg() != null && BillBean.getErrmsg().length() > 0) {
//                                callBack.onshowres(BillBean.getErrmsg());
//                            } else {
//                                callBack.onshowres(PDAUtil.connectError);
//                            }
//                            mDisposable.dispose();
//                        }
//
//                        @Override
//                        public void onNext(ResponseBody abBean) {
//                            LogUtils.w(StringProcess.responsetoString(abBean));
//                            BillBean = gson.fromJson(StringProcess.responsetoString(abBean), BillBean.class);
//                            if (BillBean != null && BillBean.getErrcode() == 0) {
//                                onComplete();
//                            } else {
//                                onError(new Throwable());
//                            }
//                        }
//                    });
//        } catch (Exception e) {
//            callBack.onshowres(PDAUtil.connectError);
//            e.printStackTrace();
//        }
    }

    @Override
    public void fb(final ScanfInBean scanfInBean, final CallBack callBack) {
        try {
            RequestBody body = RequestBody.create(PDAUtil.JSON, gson.toJson(scanfInBean));
            httpConfig.finishBill(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.onshowres(PDAUtil.billFinish);
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (baseBean.getErrmsg() != null && baseBean.getErrmsg().length() > 0) {
                                callBack.onshowres(baseBean.getErrmsg());
                            } else {
                                callBack.onshowres(PDAUtil.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            baseBean = gson.fromJson(StringProcess.responsetoString(abBean), BaseBean.class);
                            if (baseBean != null && baseBean.getErrcode() == 0) {
                                BillInfo billInfo = new BillInfo();
                                billInfo.setType(PDAUtil.IN);
                                billInfo.setBillCode(scanfInBean.getBillCode());
                                DataBaseControl.deleteBill(billInfo);
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.onshowres(PDAUtil.connectError);
            e.printStackTrace();
        }
    }
}

